//
//  VersionPopView.m
//  markmap
//
//  Created by 甘世清 on 2024/4/16.
//

#import "VersionPopView.h"

@implementation VersionPopView
+(void)SQ_Show{
    NSString * appid = @"6483865713";
    NSString *urlString = [NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",appid];
    [HttpTool post:urlString paramDic:[NSMutableDictionary new] successBlock:^(BaseModel * _Nonnull httpModel) {
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.responseObject];
        [VersionPopView SQ_Show:data];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
}
+(void)SQ_Show:(NSDictionary*)data{
    NSString *  resultCount = [NSString stringWithFormat:@"%@",[data objectForKey:@"resultCount"]];
    if ([resultCount isNotEmpty]) {
        if ([resultCount intValue] <1) {
            return;
        }
    }
    NSArray * array = [[NSArray alloc]initWithArray:[data objectForKey:@"results"]];
    if (array.count <= 0) {
        return;
    }
    NSDictionary * version = [[NSDictionary alloc]initWithDictionary:array.firstObject];
    
    NSString *newVersion = [version objectForKey:@"version"];
    
    NSString *releaseNotes = [version objectForKey:@"releaseNotes"];
   
    NSString * getVersionNumber = [VersionPopView getVersionNumber];
    if (![newVersion isNotEmpty]) {
        return;
    }
    if (![getVersionNumber isNotEmpty]) {
        return;
    }
    if ([newVersion floatValue] > [getVersionNumber floatValue]) {
        [VersionPopView SQ_popView:releaseNotes];
    }
}
+ (NSString *)getVersionNumber {
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    return version;
}

+(void)SQ_popView:(NSString*)text{
    VersionPopView * vc = [[VersionPopView alloc]initWithFrame:CGRectMake(0, 0, 280, 373)];
    vc.message.text = text;
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                           parentView:nil
                                             popStyle:LSTPopStyleFade dismissStyle:LSTDismissStyleCardDropToLeft];
    pop.hemStyle = LSTHemStyleCenter;
    pop.bgAlpha = 0.3;
    [pop pop];
    LSTPopViewWK(pop)
    [vc.noBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [wk_pop dismiss];
    }];
    [vc.yesBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [wk_pop dismiss];
        NSURL *url = [NSURL URLWithString:@"https://apps.apple.com/app/id6483865713"];
           //前往苹果商店更新
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    }];
    
}
 
- (void)SQ_UIViewForSelfView{
    UIImageView * bgImage = [[UIImageView alloc]init];
    bgImage.image = SQGetImage(@"编组 5");
    [self addSubview:bgImage];
    [bgImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self addSubview:self.message];
    [self.message mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).insets(UIEdgeInsetsMake(120, 47,128, 47));
    }];
    [self addSubview:self.yesBtn];
    [self.yesBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(202);
        make.height.mas_equalTo(40);
        make.centerX.equalTo(self);
        make.bottom.equalTo(self.mas_bottom).offset(-56);
    }];
    [self addSubview:self.noBtn];
    [self.noBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(202);
        make.height.mas_equalTo(40);
        make.centerX.equalTo(self);
        make.bottom.equalTo(self.mas_bottom).offset(-8);
    }];
}

- (UILabel *)message{
    if (!_message) {
        _message = [[UILabel alloc]init];
        _message.textAlignment = NSTextAlignmentCenter;
        _message.numberOfLines = 0;
        _message.textColor = SQColor333333;
        _message.font = FONT_REGULAR(15);
    }
    return _message;
}
- (SQCustomButton *)yesBtn{
    if (!_yesBtn) {
        _yesBtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(0, 0) midmargin:0];
        _yesBtn.SQBackimageView.image = SQGetImage(@"矩形");
        _yesBtn.text = SQNSLocalString(@"立即升级");
        _yesBtn.textColor = UIColor.whiteColor;
        _yesBtn.font = FONT_MEDIUM(16);
    }
    return _yesBtn;
}
- (SQCustomButton *)noBtn{
    if (!_noBtn) {
        _noBtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(0, 0) midmargin:0];
        _noBtn.text = SQNSLocalString(@"残忍拒绝");
        _noBtn.textColor = SQColor999999;
        _noBtn.font = FONT_MEDIUM(16);
    }
    return _noBtn;

}
@end
